<?php
/* 
 * ********* CVS HEADER ********* 
 * 
 * $RCSfile: html.php,v $ 
 * $Date: 2007/04/16 20:03:54 $ 
 * $Revision: 1.13 $ 
 * 
 * ********* CVS HEADER ********* 
 */
$row = array();
$in_session_cache=0;
if(!array_key_exists('htmlcachepage',$_SESSION)) $_SESSION['htmlcachepage'] = array();
if(!array_key_exists('htmlcachecode',$_SESSION)) $_SESSION['htmlcachecode'] = array();
if(!isset($page))$page='';
if(!isset($code))$code='';
$commentaire='';
if($page=="" && $code==""){
	//$code='/';
	$code=$session['accueil'];	
}
$query_html = "html.id,html.nom,html.titre,html.code,html.description,html.login_auteur";
$query_html.= ",html.droit,html.id_menu,html.id_repertoire,html.creation,html.modification";
$query_html.= ",html.publication,html.compteur,html.affiche_compteur,html.verrou";
$query_html.= ",html.affiche_modification,html.inenv,html.publier,html.demande,html.type";
$query_html.= ",html.recommander,html.commentaire,html.nbcomment";
if($page!="")
{
	if(!array_key_exists($page,$_SESSION['htmlcachepage'])){
		$query="SELECT $query_html,html_type.edit_op AS edit_op FROM html_type,hierarchie,html WHERE html.id='$page' AND html.droit=hierarchie.id_groupe AND hierarchie.id_groupe_dessus='$session[id_groupe]'";
		$query.=" AND html_type.id=html.type";
		$result=rportalrequete($query) or rportalerreur($query,"?op=");
		if($row=rportalligne($result)){
			$_SESSION['htmlcachepage'][$page]=$row;
		}else{
			rportalerreur("LA PAGE QUE VOUS ESSAYEZ D'AFFICHER N'EXISTE PAS","?op=");
		}
	}else{
		$row=$_SESSION['htmlcachepage'][$page];
		$in_session_cache=1;
	}
}else if($code!=""){
	$old_code = $code;
	$rep=0;
	if($code=="/"){
		$code = 'index';
	}else if($code[strlen($code)-1]=='/'){
		//on essaye de voir le contenu d'un repertoire
		$code .= 'index';
		$rep=1;
	}
	if(!array_key_exists($code,$_SESSION['htmlcachecode'])){
		$query="SELECT $query_html,rportal_menu.fichier,html_type.edit_op AS edit_op FROM html_type,hierarchie,html LEFT OUTER JOIN rportal_menu ON rportal_menu.id=html.id_menu WHERE html.code='$code' AND html.droit=hierarchie.id_groupe AND hierarchie.id_groupe_dessus='$session[id_groupe]' AND html.publier='1'";
		$query.=" AND html_type.id=html.type";
		$result=rportalrequete($query) or rportalerreur($query,"?op=");
		if($row=rportalligne($result)){
			$_SESSION['htmlcachecode'][$code]=$row;
		}else{
			if($rep==1){
				header("Location: ?op=htmlnav&code=".$old_code);
				exit;
			}
			rportalerreur("LA PAGE QUE VOUS ESSAYEZ D'AFFICHER N'EXISTE PAS","?op=");
		}
	}else{
		$row=$_SESSION['htmlcachecode'][$code];
		$in_session_cache=1;
	}
}
/**
 * les variables $html_... peuvent etre utilisees dans les header et footer
 */
$html_compteur=$row['compteur']+1;
$html_titre=$row['titre'];
$html_description=$row['description'];
$html_path=$row['code'];
$html_id = $row['id'];
$html_repertoire_id = $row['id_repertoire'];
$page_id = $row['id'];
$html_nbcomment = $row['nbcomment'];
$html_type = $row['type'];
$commentaire=$row['commentaire'];

if($in_session_cache==0){
	$query="UPDATE html SET compteur='$html_compteur' WHERE id='$page_id'";
	@rportalrequete($query) or rportalerreur($query,"?op=");
}

//Desactive jusqu'a nouvel ordre
//$session['menu']=$row['fichier'];
// On cree l'objet TPLN
require_once("module/TPLN/TPLN.php"); 
$TPLN = new TPLN; 
$TPLN->Open($op_basepath."tpl/html.html"); 

// on regarde si la page s'affiche dans l'environnement
$notinnav = !isset($innav)||(isset($innav)&&$innav!=1);
//echo "INNOTNAV=$notinnav";
if($row['inenv']==1&&$notinnav){
	$TPLN->IncludeFile("header","environnement/".$session['environnement']."/header.php"); 
	$TPLN->IncludeFile("footer","environnement/".$session['environnement']."/footer.php");
}else{
	$TPLN->Parse("header","");
	$TPLN->Parse("footer","");
}

//$new_dest=sprintf("%07d.php", $row['id']);
//$TPLN->IncludeFile("contenu","html/$new_dest");

// On affiche l'edition et on verifie le verrou
if(array_key_exists($row['edit_op'],$sessiondroit)&&$notinnav&&((($row['verrou']==$session['id_user']||$session['id_groupe']==2)&&$row['verrou']>0) || $row['verrou']==0)){
	$TPLN->Parse("edit.edit_op",$row['edit_op']);
	$TPLN->Parse("edit.pid",$row['id']);
}else{
	$TPLN->EraseBloc("edit");	
}

// SECTION DES OPTIONS
$new_dest=sprintf("%07d.php", $row['id']);
$tmpTPLN = new TPLN;
$tmpTPLN->Open("html/$new_dest");


//on affiche les commentaires
if($commentaire==1){
	if($tmpTPLN->BlocExists("cbas")){
		$query="SELECT DATE_FORMAT(date, '%d/%m/%Y') AS date, TIME_FORMAT(date, '%T') AS time,id, id_html,nom,site,texte FROM rportal_commentaire WHERE id_html='".$row['id']."' ORDER BY id ASC";
		$result=rportalrequete($query) or rportalerreur($query,"?op=");
		if(rportalnbresult($result)==0){
			$tmpTPLN->EraseBloc("cbas.cliste");
		}else{
			while($com=rportalligne($result)){
				$admin="<a href=\"?op=admhtmlcommentdel&id={$com['id']}&code=$html_path\" title=\"Supprimer ce commentaire\"><img src=\"".$rportalparams->get_value("RPORTAL","contenu.navigateur.images.supprimer")."\" alt=\"Supprimer ce commentaire\"/></a>";
				$tmpTPLN->Parse("cbas.cliste.site",$com['site']);
				$tmpTPLN->Parse("cbas.cliste.nom",$com['nom']);
				$tmpTPLN->Parse("cbas.cliste.texte",$com['texte']);
				$tmpTPLN->Parse("cbas.cliste.date",$com['date']);
				$tmpTPLN->Parse("cbas.cliste.time",$com['time']);
				if((array_key_exists($row['edit_op'],$sessiondroit))&&(array_key_exists('admhtmlcommentdel',$sessiondroit))){
					$tmpTPLN->Parse("cbas.cliste.del",$admin);
				}else{
					$tmpTPLN->Parse("cbas.cliste.del","");	
				}
				$tmpTPLN->Loop("cbas.cliste");
			}
		}
		$tmpTPLN->Parse("cbas.code",$html_path);
	}
}else{
	if($tmpTPLN->BlocExists("cbas")){
		$tmpTPLN->EraseBloc("cbas");
	}
}


$TPLN->ParsePHP("contenu",$tmpTPLN->Output());
if($page!=""&&$notinnav){
	$TPLN->Parse("nav.rid",$row['id_repertoire']);
}else{
	$TPLN->EraseBloc("nav");
}

$TPLN->Write();
?> 